package com.zsc.externalservice.services.impl;


import com.zsc.externalservice.entity.AdsSjzlUcCstCustomer;
import com.zsc.externalservice.entity.external.CustomerDetails;
import com.zsc.externalservice.entity.external.CustomerInfo;
import com.zsc.externalservice.entity.external.CustomerInfo_2;
import com.zsc.externalservice.entity.external.SalesSummary;
import com.zsc.externalservice.entity.vo.CustomerVO;
import com.zsc.externalservice.mapper.CustomerMapper;

import com.zsc.externalservice.services.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    @Override
    public CustomerInfo findCustomerByCustUuid(String custUuid) {
        return customerMapper.findCustomerByCustUuid(custUuid);
    }

    @Override
    public CustomerInfo_2 findCustomerByCustUuid_2(String custUuid) {
        return customerMapper.findCustomerByCustUuid_2(custUuid);
    }

    @Override
    public List<SalesSummary> getOrderByCustUuid(String custUuid) {
        return customerMapper.getOrderByCustUuid(custUuid);
    }

    @Override
    public CustomerDetails getCustomerDetailsCustUuid(String custUuid) {
        return customerMapper.getCustomerDetailsCustUuid(custUuid);
    }


    /**
     * 根据用户uuid获取用户信息
     *
     * @param custUuid 用户uuid
     * @return CustomerVO
     */
    @Override
    public CustomerVO getCustomerInfo(String custUuid) {
        if (custUuid == null) return null;

        AdsSjzlUcCstCustomer a = customerMapper.getCustomerByCustUuid(custUuid);

        CustomerVO result = CustomerVO.builder()
                .custUuid(a.getCustUuid())
                .licenseCode(a.getLicenseCode())
                .custCode(a.getCustCode())
                .custName(a.getCustName())
                .address(a.getAddress())
                .cigarGrade(a.getCigaTypeName() + "/" + a.getCigaTypeNameB() + "/" + a.getCigaTypeNameC() + "/" + a.getCigaTypeNameD())
                .licenseEndDate(a.getLicenseEndDate())
                .busiPlace(a.getBusiPlaceCodeText())
                .custTypeName(a.getCustTypeName())
                .terminalLevel(a.getTerminalLevelText())
                .build();
        log.info("查询到的数据是：{}", result);
        return result;
    }

    /**
     * 获取所用一般零售用户
     *
     * @return
     */
    @Override
    public List<CustomerVO> getAllCustomer() {
        LocalDate date = LocalDate.now().minusDays(1);
        List<CustomerVO> result = customerMapper.getAllCustomers(date);
        return result;
    }

    /**
     * 根据客户经理名称获取客户经理的uuid
     *
     * @param name
     * @return
     */
    @Override
    public String getManagerByName(String name) {
        AdsSjzlUcCstCustomer adsSjzlUcCstCustomer = customerMapper.getMangerUuid(name);
        return adsSjzlUcCstCustomer.getCustManagerPersonUuid();
    }

    /**
     * 根据许可证号获取客户信息
     *
     * @param licenseCode
     * @return
     */
    @Override
    public CustomerVO getCustomerByLicenseCode(String licenseCode) {
        AdsSjzlUcCstCustomer a = customerMapper.getCustomerByLicenseCode(licenseCode);
        CustomerVO customerVO = CustomerVO.builder()
                .custUuid(a.getCustUuid())
                .licenseCode(a.getLicenseCode())
                .custCode(a.getCustCode())
                .custName(a.getCustName())
                .address(a.getAddress())
                .cigarGrade(a.getCigaTypeName() + "/" + a.getCigaTypeNameB() + "/" + a.getCigaTypeNameC() + "/" + a.getCigaTypeNameD())
                .licenseEndDate(a.getLicenseEndDate())
                .busiPlace(a.getBusiPlaceCodeText())
                .custTypeName(a.getCustTypeName())
                .terminalLevel(a.getTerminalLevelText())
                .managePersonName(a.getManagePersonName())
                .build();
        return customerVO;
    }
}
